{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-30T03:03:19.552817Z",
     "start_time": "2025-05-30T03:03:18.917490Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.metrics import silhouette_score\n",
    "from sklearn.cluster import KMeans\n",
    "import numpy as np\n",
    "\n",
    "# 生成示例数据\n",
    "X = np.random.rand(100, 2)\n",
    "\n",
    "# 使用KMeans进行聚类\n",
    "kmeans = KMeans(n_clusters=3)\n",
    "kmeans.fit(X)\n",
    "\n",
    "# 计算轮廓系数\n",
    "score = silhouette_score(X, kmeans.labels_)\n",
    "print(\"Silhouette Coefficient:\", score)"
   ],
   "id": "126ad9a0587d5471",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Silhouette Coefficient: 0.44962415580563936\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\PythonProject\\Anaconda3-2024\\envs\\test\\lib\\site-packages\\sklearn\\cluster\\_kmeans.py:1416: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning\n",
      "  super()._check_params_vs_input(X, default_n_init=10)\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-30T03:03:19.599818Z",
     "start_time": "2025-05-30T03:03:19.563818Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.metrics import davies_bouldin_score\n",
    "from sklearn.cluster import KMeans\n",
    "import numpy as np\n",
    "\n",
    "# 生成示例数据\n",
    "X = np.random.rand(100, 2)\n",
    "\n",
    "# 使用KMeans进行聚类\n",
    "kmeans = KMeans(n_clusters=3)\n",
    "kmeans.fit(X)\n",
    "\n",
    "# 计算戴维斯-布尔丁指数\n",
    "score = davies_bouldin_score(X, kmeans.labels_)\n",
    "print(\"Davies-Bouldin Index:\", score)"
   ],
   "id": "59484caf7729a85c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Davies-Bouldin Index: 0.807166802462988\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\PythonProject\\Anaconda3-2024\\envs\\test\\lib\\site-packages\\sklearn\\cluster\\_kmeans.py:1416: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning\n",
      "  super()._check_params_vs_input(X, default_n_init=10)\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-30T03:03:19.956368Z",
     "start_time": "2025-05-30T03:03:19.911371Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.metrics import adjusted_rand_score\n",
    "from sklearn.cluster import KMeans\n",
    "import numpy as np\n",
    "\n",
    "# 生成示例数据\n",
    "X = np.random.rand(100, 2)\n",
    "y = np.random.randint(0, 2, size=100)  # 假设真实标签\n",
    "\n",
    "# 使用KMeans进行聚类\n",
    "kmeans = KMeans(n_clusters=2)\n",
    "kmeans.fit(X)\n",
    "\n",
    "# 计算兰德指数\n",
    "score = adjusted_rand_score(y, kmeans.labels_)\n",
    "print(\"Adjusted Rand Index:\", score)"
   ],
   "id": "a6bbfd89a8556c4a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adjusted Rand Index: -0.01078167115902965\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\PythonProject\\Anaconda3-2024\\envs\\test\\lib\\site-packages\\sklearn\\cluster\\_kmeans.py:1416: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning\n",
      "  super()._check_params_vs_input(X, default_n_init=10)\n"
     ]
    }
   ],
   "execution_count": 3
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
